package cn.tedu.csmall.server.web;

import cn.tedu.csmall.server.exception.ServiceException;
import lombok.Data;

import java.util.Objects;

/**
 * @author lixin
 * @Date 2022/9/6
 */
@Data
public class JsonResult {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 状态信息
     */
    private String message;

    /**
     * 处理成功时需要响应到客户端的数据
     */
    private Object data;

    public static JsonResult ok() {
        return JsonResult.ok(null);
    }

    public static JsonResult ok(Object data) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = ServiceCode.OK;
        jsonResult.data = data;
        return jsonResult;
    }



    public static JsonResult fail(ServiceException serviceException) {
        return JsonResult.fail(serviceException.getServiceCode(), serviceException.getMessage());
    }

    public static JsonResult fail(Integer code, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = code;
        jsonResult.message = message;
        return jsonResult;
    }
}
